package com.bcnapps.DemoGame;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;


public class GameThree extends SurfaceView {

	private SurfaceHolder holder;
	private GThreeDrawThread gtdt;
	
	//classes auxiliars
	private Nuvols nuvols;
	//control thread
	private boolean surfacecreated;
	private boolean threadrunning;
	//fons
	Paint fons;
	Paint enllas;
	Paint bola;
	// ***** joc ***** //
	//multitouch
	private boolean d0, d1;
	private float d0x, d0y, d1x, d1y;
	//bola
	private float bx, by, br, bvx, bvy; //posicio x, posicio y, radi, velocitat x, velocitat y
	
	
	public GameThree(Context context) {
		super(context);
		init();
	}

	public GameThree(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public GameThree(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}
	
	private void init() {
		surfacecreated = false;
		threadrunning = false;
		holder = getHolder();
		holder.addCallback( new Callback() {
			
			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
				stopthread();
				surfacecreated = false;
			}
			
			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				holder.setFormat(PixelFormat.RGBA_8888);
				
				fons = new Paint(); 
			    Shader shader = new LinearGradient(0,0,GV.Screen.metrics.widthPixels,GV.Screen.metrics.heightPixels,new int[]{0xFFA4D8FF,0xFF0031D2},null,Shader.TileMode.CLAMP);
			    fons.setShader(shader);
			    
			    enllas = new Paint();
			    enllas.setColor(0xFFFF0000);
			    enllas.setStrokeWidth(5f);
			    
			    bola = new Paint();
			    bola.setColor(0xFF00FF00);
				
				nuvols = new Nuvols(getResources());
				surfacecreated = true;
				
				d0 = d1 = false;
				
				br = GV.widtthpercent(0.05f);
				bx = GV.widtthpercent(0.5f);
				by = br;
				bvx = bvy = 0;
			}
			
			@Override
			public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
				startthread();
			}
		});  
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawRect(0,0,getWidth(),getHeight(), fons);
		nuvols.actualitza();
		nuvols.draw(canvas);
		actualitza_bola();
		if(d0 && d1) {
			colisions();
			canvas.drawLine(d0x, d0y, d1x, d1y, enllas);
		}
		canvas.drawCircle(bx, by, br, bola);
	}
	
	//fetes amb la part posterior mitja del cos (amb el cul)
	public void colisions() {
		if(dist(d0x,d0y,bx,by)+dist(bx,by,d1x,d1y)-dist(d0x,d0y,d1x,d1y)<br) {
			 float cx = d0x - d1x;
			 float cy = d0y - d1y;
			 bvx -= cy*0.05;
			 bvy += cx*0.05;
		}
	}
	
	private void actualitza_bola() {
		bvy += GV.heightpercent(0.003f); //gravetat
		bvx = bvx*0.95f;
		bx += bvx;
		by += bvy;
		if(by > GV.heightpercent(1f)+br || bx < -br || bx > GV.widtthpercent(1f)+br) {
			br = GV.widtthpercent(0.05f);
			bx = GV.widtthpercent(0.5f);
			by = br;
			bvx = bvy = 0;
		}
	}
	
	public float dist(float ax,float ay,float bx,float by) {
		return (float)Math.sqrt((ax-bx)*(ax-bx)+(ay-by)*(ay-by));
	}
	
	public void startthread() {
		if(!surfacecreated) return;
		if(threadrunning) return;
		gtdt = new GThreeDrawThread(this);
		gtdt.setRunning(true);
		gtdt.start();
		threadrunning = true;
	}
	
	public void stopthread() {
		if(!surfacecreated) return;
		if(!threadrunning) return;
		boolean retry = true;
		while (retry) {
			try {
				gtdt.setRunning(false);
				gtdt.join();
				threadrunning = false;
				retry = false;
				
			} catch (InterruptedException e) {}
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getActionMasked() == MotionEvent.ACTION_DOWN) {
			d0 = true;
			d0x = event.getX(0);
			d0y = event.getY(0);
			return true;
		}
		if(event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN) {
			if(event.getActionIndex()!=1) return true;
			d1 = true;
			d1x = event.getX(1);
			d1y = event.getY(1);
			return true;
		}
		if(event.getActionMasked() == MotionEvent.ACTION_UP) {
			d0 = false;
			return true;
		}
		if(event.getActionMasked() == MotionEvent.ACTION_POINTER_UP) {
			if(event.getActionIndex()!=1) return true;
			d1 = false;
			return true;
		}
		d0x = event.getX(0);
		d0y = event.getY(0);
		d1x = event.getX(1);
		d1y = event.getY(1);
		
		return super.onTouchEvent(event);
	}
}